﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PluginFramework.Common;
using QLBH.Model.Controller;
using System.Threading;
using System.Windows.Forms;
using QLBH.DataModel;
using DevComponents.DotNetBar;
using QLBH.Category.Presenter;
using QLBH.StandardUI;
using QLBH.FeatureBase.Modules.Category;
using QLBH.FeatureBase;
using QLBH.Base;
using QLBH.ModelInterface;

namespace QLBH.Category
{
    [ExtensionOf(typeof(StaffManagementToolExtensionPoint))]
    public class QLNhanVienTool : BaseView, IStaffmangementTool
    {
        public QLNhanVienTool()
            :base()
        {

        }
        public FormViewBase GetView(object arg)
        {
            return new QLNhanVienTool();
        }
        public override void AddNew(object sender, EventArgs e)
        {
            try
            {
                AddNhanVienDlg addnew = new AddNhanVienDlg();
                if (addnew.ShowDialog() == DialogResult.OK)
                {
                    RefreshData(sender, e);
                }
                base.AddNew(sender, e);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "QL Nhân viên");
                return;
            }
            
        }
        public override void EditItem(object sender, EventArgs e)
        {
            try
            {
                AddNhanVienDlg editItem = new AddNhanVienDlg();
                editItem.DlgType = eDlgType.EDIT;
                var NhanVienmodel = DataGridView.SelectedRows[0].DataBoundItem as IQLNhanVienModel;
                var model = NhanVienmodel.ToEntity();
                editItem.Model = model;
                if (editItem.ShowDialog() == DialogResult.OK)
                {
                    RefreshData(sender, e);
                }
                base.EditItem(sender, e);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "QL Nhân viên");
                return;
            }
           
        }
        public override void DeleteItem(object sender, EventArgs e)
        {
            try
            {
                var nhanvienModel = DataGridView.SelectedRows[0].DataBoundItem as IQLNhanVienModel;
                var model =  nhanvienModel.ToEntity();
                NhanVienController.GetInstance().Delete(model);
                RefreshData(sender, e);
                base.DeleteItem(sender, e);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "QL Nhân viên");
                return;
            }
           
        }
        public override void RefreshData(object sender, EventArgs e)
        {
            base.RefreshData(sender, e);
            LoadDSNhanVien();
        }
        public override void BuildSearchMenu()
        {
            ButtonItem btnSearchByID = new ButtonItem("btnSearchByID", "Tìm theo Mã nhân viên");
            ButtonItem btnSearchByName = new ButtonItem("btnSearchByName", "Tìm theo Tên nhân viên");
            

            BtnSearch.SubItems.Add(btnSearchByID);
            BtnSearch.SubItems.Add(btnSearchByName);
           

            btnSearchByID.Click += btnSearchByID_Click;

            btnSearchByName.Click += btnSearchByName_Click;
            
            base.BuildSearchMenu();
        }
        void btnSearchByName_Click(object sender, EventArgs e)
        {
            List<IQLNhanVienModel> source = (List<IQLNhanVienModel>)DataSource;

            source = source.Where(m => m.TENNV.Contains(TbxSearchInput.ControlText)).ToList();

            DataGridView.DataSource = source;
        }

        void btnSearchByID_Click(object sender, EventArgs e)
        {
            List<IQLNhanVienModel> source = (List<IQLNhanVienModel>)DataSource;

            source = source.Where(m => m.MANV.Contains(TbxSearchInput.ControlText)).ToList();

            DataGridView.DataSource = source;
        }
        public override void LoadForm(object sender, EventArgs e)
        {
            base.LoadForm(sender, e);

            LoadDSNhanVien();
        }
        private void LoadDSNhanVien()
        {
            try
            {
                QLNhanVienPresenter nhanvienPresenter = new QLNhanVienPresenter();
                var items = nhanvienPresenter.GetDSNhanVien();
                DataSource = items;

                DataGridView.DataSource = DataSource;                

                DataGridView.Columns["MANV"].HeaderText = "Mã NV";
                DataGridView.Columns["MANV"].DisplayIndex = 0;

                DataGridView.Columns["TENNV"].HeaderText = "Tên NV";
                DataGridView.Columns["TENNV"].DisplayIndex = 1;

                DataGridView.Columns["NGAYSINH"].HeaderText = "Ngày Sinh";
                DataGridView.Columns["NGAYSINH"].DisplayIndex = 2;
                DataGridView.Columns["NGAYSINH"].DefaultCellStyle.Format =
                    Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern;

                DataGridView.Columns["QUEQUAN"].HeaderText = "Quê Quán";
                DataGridView.Columns["QUEQUAN"].DisplayIndex = 3;

                DataGridView.Columns["DIACHI"].HeaderText = "Địa Chỉ";
                DataGridView.Columns["DIACHI"].DisplayIndex = 4;

                DataGridView.Columns["SDT"].HeaderText = "SĐT";
                DataGridView.Columns["SDT"].DisplayIndex = 5;

                DataGridView.Columns["DIDONG"].HeaderText = "Di động";
                DataGridView.Columns["DIDONG"].DisplayIndex = 6;

                DataGridView.Columns["CMTND"].HeaderText = "CMTND";
                DataGridView.Columns["CMTND"].DisplayIndex = 7;

                DataGridView.Columns["EMAIL"].HeaderText = "Email";
                DataGridView.Columns["EMAIL"].DisplayIndex = 8;

                DataGridView.Columns["LUONGCB"].HeaderText = "Lương CB";
                DataGridView.Columns["LUONGCB"].DisplayIndex = 9;

                DataGridView.Columns["GHICHU"].HeaderText = "Ghi Chú";
                DataGridView.Columns["GHICHU"].DisplayIndex = 10;

                DataGridView.Columns["NGAYTAO"].HeaderText = "Ngày Tạo";
                DataGridView.Columns["NGAYTAO"].DisplayIndex = 11;
                DataGridView.Columns["NGAYTAO"].DefaultCellStyle.Format =
                    Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern;

                DataGridView.Columns["USERNAME"].Visible = false;
                DataGridView.Columns["USERNAME"].DisplayIndex = 12;

                DataGridView.Columns["NVID"].Visible = false;
                DataGridView.Columns["NVID"].DisplayIndex = 13;

                DataGridView.Update();

            }
            catch (System.Exception ex)
            {
                ShowMessage.Instance.ShowError(ex.Message);
            }                          
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // QLNhanVienTool
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
            this.Name = "QLNhanVienTool";
            this.Size = new System.Drawing.Size(943, 560);
            this.ResumeLayout(false);

        }
    }
}
